package com.nowcoder.code.lcof;

public class JZ49 {


    public static void main(String[] args) {
        System.out.println(new JZ49().GetUglyNumber_Solution(7));

    }

    public int GetUglyNumber_Solution(int index) {
        if (index == 0) {
            return 0;
        }
        int i = 5, i2 = 2, i3 = 2, i5 = 4;
        int[] is = new int[index + 5];
        is[0] = 1;
        is[1] = 2;
        is[2] = 3;
        is[3] = 4;
        is[4] = 5;

        if (index <= 5) {
            return is[index - 1];
        }
        while (i != index) {
            int n2 = is[i2] * 2;
            int n3 = is[i3] * 3;
            int n5 = is[i5] * 5;

            if (n2 <= n3 && n2 <= n5) {
                is[i] = n2;
                i2++;
                if (n2 == n3) {
                    i3++;
                }
                if (n2 == n5) {
                    i5++;
                }
            } else if (n3 <= n2 && n3 <= n5) {
                is[i] = n3;
                i3++;

                if (n3 == n2) {
                    i2++;
                }
                if (n3 == n5) {
                    i5++;
                }
            } else if (n5 <= n3 && n5 <= n2) {
                is[i] = n5;
                i5++;
                if (n5 == n3) {
                    i3++;
                }
                if (n5 == n2) {
                    i2++;
                }
            }
            i++;
        }

        return is[i - 1];
    }

}
